package com.xwh.shop.user.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xwh.shop.user.model.dto.MemberInfoPageQueryDTO;
import com.xwh.shop.user.model.entity.MemberInfo;
import com.xwh.shop.user.model.vo.MemberInfoPageVO;
import com.xwh.shop.user.service.mapper.MemberInfoMapper;
import com.xwh.shop.user.service.service.MemberInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xwh
 * @since 2025-07-15
 */
@Slf4j
@Service
public class MemberInfoServiceImpl extends ServiceImpl<MemberInfoMapper, MemberInfo> implements MemberInfoService {


//    todo 优化getPage
    @Override
    public IPage<MemberInfoPageVO> getPage(MemberInfoPageQueryDTO memberInfoPageQueryDTO) {
        /*
        mybatis-plus分页支持的泛型是Member->Page<Member>
        但是这里需要的是Page<MemberPageVO>，所以需要 Page<Member> 转成 Page<MemberPageVO>
         */
        LambdaQueryWrapper<MemberInfo> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(StringUtils.hasText(memberPageQueryDTO.getMobile()), MemberInfo::getMobile, memberPageQueryDTO.getMobile());
//        wrapper.eq(memberPageQueryDTO.getStatus() != null, MemberInfo::getStatus, memberPageQueryDTO.getStatus());

        Page<MemberInfo> page = this.page(new Page<>(memberInfoPageQueryDTO.getCurrent(), memberInfoPageQueryDTO.getPageSize()), wrapper);
        //分析page的结构得到一个结论，转换类型只需要考虑records
        List<MemberInfo> records = page.getRecords();
        //创建一个page对象，用于返回
        Page<MemberInfoPageVO> pageVO = new Page<>();
        //数据对拷 -> 会有一个问题，pageVO里面的records会被page中的records覆盖
        BeanUtils.copyProperties(page,pageVO);
        //恢复pageVO中的records
        List<MemberInfoPageVO> voList=new ArrayList<>();
        //遍历records，将Member对象转换成MemberPageVO对象
        for (MemberInfo member : records) {
            MemberInfoPageVO memberInfoPageVO = new MemberInfoPageVO();
            BeanUtils.copyProperties(member, memberInfoPageVO);
            //需要一个List<MemberPageVO>对象接收改造好的memberPageVO对象,没有则需要创建
            voList.add(memberInfoPageVO);
        }
        pageVO.setRecords(voList);
        log.info("page:{}", page);
        return pageVO;
    }

    @Override
    public void resetPassword(Long id) {

    }

    @Override
    public void enable(Long id) {

    }
}
